package com.wxzz.elearning.common.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelTopicUtils {

    public Workbook getExcel(MultipartFile file) throws IOException {
        Workbook wb = null;
        if (file.getBytes() == null) {
            System.out.println("文件不存在");
            wb = null;
        } else {
            String originalFilename = file.getOriginalFilename();
            String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));//获得后缀名
            try {
                InputStream is = file.getInputStream();
                if (".xls".equals(fileType)) {
                    wb = new HSSFWorkbook(is);
                } else if (".xlsx".equals(fileType)) {
                    wb = new XSSFWorkbook(is);
                } else {
                    System.out.println("格式不正确");
                    wb = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return wb;
    }

    public Integer format(String s) {
        if (s.contains(".")) {
            String[] s1 = s.split("\\.");
            return Integer.valueOf(s1[0]);
        }
        return Integer.valueOf(s);
    }

    public Long formatLong(double d) {
        DecimalFormat df = new DecimalFormat("0");
        String val = df.format(d);
        return Long.valueOf(val);
    }
}
